/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.physics;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Joint;
import com.badlogic.gdx.physics.box2d.joints.DistanceJoint;
import com.badlogic.gdx.physics.box2d.joints.DistanceJointDef;
import com.jdiv.Const;
import com.jdiv.kernel.JDivSystem;

/** A distance joint constrains two points on two bodies to remain at a fixed distance from each other. You can view this as a massless, rigid rod. */ 

public class JointDistance {
	
	DistanceJointDef jointDef;
	Joint joint;
	
	/**
	 * Constructor of JointDistance.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	
	public JointDistance( Body bodyA, Body bodyB, boolean collideConnected ){
		
		jointDef = new DistanceJointDef();
		
		joint = null;
		
		init( bodyA, bodyB, collideConnected );
	}
	
	/**
	 * Initialize the bodies.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	
   public void init ( Body bodyA, Body bodyB, boolean collideConnected ){
		
		if( joint != null ) return;
		
		jointDef.bodyA = bodyA;
		jointDef.bodyB = bodyB;
		jointDef.collideConnected = collideConnected;
	}
   
   /**
    * Sets the pivot point from object A.
    * @param x Coordinate X.
    * @param y Coordinate Y.
    */
	
	public void setAnchorA( float x, float y ){
		jointDef.localAnchorA.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
				                  y * Const.BOX2D_SCREEN_TO_PHYSICS);
	}
	
	/**
	    * Sets the pivot point from object B.
	    * @param x Coordinate X.
	    * @param y Coordinate Y.
	    */
		 
	public void setAnchorB( float x, float y ){
			jointDef.localAnchorB.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
					                  y * Const.BOX2D_SCREEN_TO_PHYSICS);
	}
	
	/**
	 * @param dampingRatio The damping ratio.
	 */
	
	public void setDampingRatio( float dampingRatio ){
		jointDef.dampingRatio = dampingRatio; 
	}
	
	/**
	 * @param frequencyHz The mass-spring-damper frequency in Hertz.
	 */
	
	public void setFrequencyHz( float frequencyHz ){
		jointDef.frequencyHz = frequencyHz; 
	}
	
	/**
	 * @param length The natural length between the anchor points.
	 */
	
	public void setLength( float length ){
		jointDef.length = length * Const.BOX2D_SCREEN_TO_PHYSICS;
	}
	
	/**
	 * Add the joint to the physical world.
	 * @return The joint in the physical world.
	 */
	
	public Joint addToWorld(){
		 if( joint != null ) return joint;
		 
		 if( JDivSystem.box2dWorld != null )	
		  joint = JDivSystem.box2dWorld.createJoint(jointDef);
		 
		 return joint;
	}
	
	/**
	 * @return Gets DistanceJoint of LibGdx.
	 */
	
	public DistanceJoint getDistanceJoint(){
		return (DistanceJoint)joint;
	}
	
	/** Delete the joint in the physical world. */
	
	public void delete(){
		if( (JDivSystem.box2dWorld == null) || (joint == null) ) return;
		
		JDivSystem.box2dWorld.destroyJoint(joint);
		joint = null;
	}

}
